<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container" v-if="!loading">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ artObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="artObj.aut_name" :label="fromatTimeAgoFn(artObj.pubdate)">
        <template #icon>
          <img :src="artObj.aut_photo" alt="avatar" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button @click="followOrUnFollowFn(true)" v-if="artObj.is_followed" type="info" size="mini">已关注</van-button>
            <van-button @click="followOrUnFollowFn(false)" v-else icon="plus" type="info" size="mini" plain>关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider>Start</van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box" id="like-box">
        <van-button @click="likeOrDislikeFn(1)" v-if="artObj.attitude === 1" icon="good-job" type="danger" size="small">已点赞</van-button>
        <van-button @click="likeOrDislikeFn(-1)" v-else icon="good-job-o" type="danger" plain size="small">点赞</van-button>
      </div>
    </div>

    <!-- 文章评论 -->
    <!-- artObj.art_id刚开始没有值，会报错，所以需要判断一下 -->
    <!-- 父接收 -->
    <Comment :artObj="artObj" v-if="artObj.art_id" :artId="artObj.art_id"></Comment>
  </div>
</template>

<script>
// 导入接口
import { likeArtAPI, dislikeArtAPI, followAutAPI, unFollowAutAPI, getArticleDetailAPI } from '@/api/article'
import { Toast } from 'vant'

import Comment from './comment'

export default {
  name: 'ArticleDetail',
  data () {
    this.toast = Toast.loading({
      message: '加载中...'
    })
    return {
      loading: true, // 详情页数据家在状态 true加载中
      // 文章对象：默认就是对象
      artObj: {}
    }
  },
  components: {
    Comment
  },
  // created 和 activated 的区别
  // created 组件创建时触发一次
  // activated 专门配合keep-alive使用，每次切换激活时被触发
  async activated () {
    const resp = await getArticleDetailAPI({
      artId: this.$route.query.art_id
    })
    this.loading = false
    // 手动关闭
    if (!this.loading) {
      this.toast.clear()
    }
    // console.log(resp)
    this.artObj = resp.data
  },
  methods: {
    // 作者关注/取关
    async followOrUnFollowFn (isFollowed) {
      // 1 通过布尔值 控制关注状态取反 操作vue变量
      this.artObj.is_followed = !this.artObj.is_followed
      // 2 结果持久化， 同步到后台， 刷新后状态依然存在
      try {
        // 三元表达式判断关注结果
        isFollowed ? await unFollowAutAPI({
          autId: this.artObj.aut_id
        }) : await followAutAPI({
          autId: this.artObj.aut_id
        })
        // 成功提示信息
        Toast.success(isFollowed ? '取关成功' : '关注成功')
      } catch (error) {
        // 失败提示
        Toast.fail(isFollowed ? '取关失败' : '关注失败')
      }
    },
    // 点赞/已点赞
    async likeOrDislikeFn (attitude) {
      // 控制取反
      this.artObj.attitude = this.artObj.attitude === 1 ? -1 : 1
      try {
        // 调用接口
        // 此处 1 代表已点赞 -1 代表 未点赞
        attitude === 1 ? await dislikeArtAPI({
          artId: this.artObj.art_id
        }) : await likeArtAPI({
          artId: this.artObj.art_id
        })
        // 成功提示
        Toast.success(attitude === 1 ? '取消点赞成功' : '点赞成功')
      } catch (error) {
        // 失败提示
        Toast.success(attitude === 1 ? '取消点赞失败' : '点赞失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
    width: 100%;
  }
    /deep/ pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
